<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>杭州房间管理</el-breadcrumb-item>
      <el-breadcrumb-item>杭州房间列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-form
      :inline="true"
      :model="form"
      label-width="120px"
      @submit.native.prevent
    >
      <el-form-item label="查询杭州房间">
        <el-input
          prefix-icon="el-icon-search"
          @keyup.native.enter="queryRoom"
          v-model="form.roomId"
          placeholder="请输入杭州房间ID"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="queryRoom" type="primary" round>搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="room" style="width: 100%">
      <el-table-column label="杭州房间ID" prop="roomId"></el-table-column>
      <el-table-column
        label="杭州房间名称"
        prop="roomTypeName"
      ></el-table-column>
      <el-table-column label="杭州房间价格" prop="roomPrice"></el-table-column>
      <el-table-column label="是否被使用">
        <template slot-scope="scope">
          <el-switch
            style="display: block"
            v-model="scope.row.isUsed"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="是"
            inactive-text="否"
            active-value="是"
            inactive-value="否"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            @click="delRoom(scope.row.roomId)"
            circle
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="el-icon-view"
            circle
          ></el-button>
          <el-button
            @click="modifyRoom(scope.row.roomId)"
            type="success"
            size="small"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button
            type="warning"
            size="small"
            icon="el-icon-cold-drink"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="pageChange"
      background
      :page-size="pagesize"
      :current-page="page"
      layout="->,prev, pager, next"
      :total="sum"
    >
    </el-pagination>
    <el-dialog title="修改房间信息" :visible.sync="dialogFormVisible">
      <el-form ref="form" :rules="rules" :model="form" label-width="120px">
        <el-form-item label="房间种类名称" prop="roomTypeName">
          <el-input v-model="form.roomTypeName"></el-input>
        </el-form-item>
        <el-form-item label="房间价格" prop="roomPrice">
          <el-input v-model="form.roomPrice"></el-input>
        </el-form-item>
        <el-form-item label="是否售出" prop="isUsed">
          <el-switch
            style="display: block"
            v-model="form.isUsed"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="是"
            inactive-text="否"
            active-value="是"
            inactive-value="否"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" @click="dialogFormVisible = false"
          >取 消</el-button
        >
        <el-button type="primary" @click="confirmModify">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
export default {
  data() {
    return {
      dialogFormVisible: false,
      form: {
        roomId: "",
        roomTypeName: "",
        roomPrice: "",
        isUsed: "",
      },
      room: [],
      pagesize: 6,
      page: 1,
      sum: 0,
      rules: {
        roomTypeName: [
          { required: true, message: "房间种类名称不能为空", trigger: "blur" },
        ],
        roomPrice: [
          { required: true, message: "房间价格不能为空", trigger: "blur" },
        ],
        isUsed: [
          { required: true, message: "售出情况不能为空", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    //加载杭州房间列表  分页查询
    let params = { page: this.page, pagesize: this.pagesize };
    this.$http.hangzhouApi
      .pageList(params)
      .then((res) => {
        //console.log(res);
        this.room = res.data.data[0];
        this.sum = res.data.data[1][0].sum;
      })
      .catch((err) => console.log(err));
  },
  methods: {
    // 通过id查杭州房间
    queryRoom() {
      if (this.form.roomId) {
        this.$http.hangzhouApi
          .queryById(this.form.roomId)
          .then((res) => {
            //console.log(res);
            this.room = res.data.data;
          })
          .catch((err) => console.log(err));
      } else {
        this.loadRoom();
      }
    },
    // 加载杭州房间列表
    loadRoom() {
      //加载杭州房间列表  分页查询
      let params = { page: this.page, pagesize: this.pagesize };
      this.$http.hangzhouApi
        .pageList(params)
        .then((res) => {
          //console.log(res);
          this.room = res.data.data[0];
          this.sum = res.data.data[1][0].sum;
        })
        .catch((err) => console.log(err));
    },
    //删除杭州房间
    delRoom(bid) {
      this.$http.hangzhouApi
        .delete({ roomId: bid })
        .then((res) => {
          //console.log(res);
          if (res.data.code == 200) {
            this.$message({
              type: "success",
              message: "删除杭州房间成功",
            });
            this.loadRoom();
          }
        })
        .catch((err) => console.log(err));
    },
    // 页码改变时，列表也变化
    pageChange(page) {
      //console.log(page);
      this.page = page;
      let params = { page, pagesize: this.pagesize };
      this.$http.hangzhouApi
        .pageList(params)
        .then((res) => {
          //console.log(res);
          this.room = res.data.data[0];
          this.sum = res.data.data[1][0].sum;
        })
        .catch((err) => console.log(err));
    },
    modifyRoom(id) {
      this.dialogFormVisible = true;
      this.$store.commit("setRoomId", id);
      // 信息回填
      this.$http.hangzhouApi
        .queryById(id)
        .then((res) => {
          //console.log(res);
          this.form = res.data.data[0];
        })
        .catch((err) => console.log(err));
    },
    // 确认修改
    confirmModify() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let params = {
            roomId: this.$store.state.roomId,
            roomTypeName: this.form.roomTypeName,
            roomPrice: this.form.roomPrice,
            isUsed: this.form.isUsed,
          };
          this.$http.hangzhouApi
            .modify(params)
            .then((res) => {
              //console.log(res);
              if (res.data.code == 200) {
                this.$message({
                  type: "success",
                  message: "修改房间信息成功！",
                });
                this.$refs["form"].resetFields();
                this.dialogFormVisible = false;
                this.loadRoom();
              }
            })
            .catch((err) => console.log(err));
        } else {
          this.$message({
            type: "error",
            message: "表单填写错误！",
          });
        }
      });
    },
  },
};
</script>
  
  <style lang="scss" >
.el-pagination__rightwrapper {
  margin-top: 30px;
  margin-right: 100px;
}
</style>
  <style lang="scss">
.el-dialog__title {
  color: #409eff;
  font-size: 27px;
  font-weight: bold;
}
.el-form-item__label {
  color: orangered;
}
</style>